#!/usr/bin/env python
# -*- coding: utf-8 -*-

# TODO: Tag website resource with firefox tags if the link is a bookmark
import dbus
import sys
import os
import subprocess
import gettext

from PyQt4.QtCore import QString, QUrl, QCoreApplication
from PyKDE4.nepomuk import Nepomuk
from PyKDE4.soprano import Soprano

# no Nepomuk::Vocabulary in python
pimo_isRelated = "http://www.semanticdesktop.org/ontologies/2007/11/01/pimo#isRelated"
nfo_website = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Website"
tmo_task = "http://www.semanticdesktop.org/ontologies/2008/05/20/tmo#Task"
nao_prefLabel = "http://www.semanticdesktop.org/ontologies/2007/08/15/nao#prefLabel"
nexif_geo = "http://www.semanticdesktop.org/ontologies/2007/05/10/nexif#geo"
notif = ""
ntcommands = ["nepotaskrelatedtags", "nepotaskemitnotif"]
########################################################
# get current active task
########################################################
def getCurrentContext():
	sb = dbus.SessionBus()
	proxy = sb.get_object(
			'org.kde.nepomuk.services.nepomukusercontextservice', 
			'/nepomukusercontextservice')
	return QUrl.fromEncoded(proxy.currentUserContext())

########################################################
# add relation between website and active task, if needed
########################################################
def addUrlToTask(ct, url, title):
	app = QCoreApplication(sys.argv)
	app.setApplicationName(sys.argv[0])
	Nepomuk.ResourceManager.instance().init()

	resws = Nepomuk.Resource(url, QUrl(nfo_website))
	resct = Nepomuk.Resource(ct, QUrl(tmo_task))

	if not(resws.exists()):
		resws.pimoThing()
		resws.setLabel(title);
		resws.addSymbol("text-html")

	if resct.exists():
		resws.addProperty(QUrl(pimo_isRelated), Nepomuk.Variant(resct))
		tl = resct.genericLabel()
		global notif
		notif = _("Page '%(title)s' properly added to task '%(tasklabel)s'.") % \
			{'title': title, 'tasklabel': tl}
		return resws
	return None

########################################################
# add a tag to website resource
########################################################
def addTagToUrl(tag, resws):
	ntag = Nepomuk.Tag(tag)
	resws.addTag(ntag)

if len(sys.argv) >= 3:
	url = QUrl.fromEncoded(sys.argv[1])
	title = QString(sys.argv[2]).trimmed()
	ct =  getCurrentContext()
	lp = sys.argv[0].replace("taskaddurl.py", "")
	gettext.install('taskaddurl', "%s/locale/" % lp, unicode = True)

if not(ct is None):
	resws = addUrlToTask(QUrl(ct), url, title)

if len(sys.argv) == 3:
	sys.exit()
else:
	argn = 3

if not(sys.argv[argn] is None) and sys.argv[argn] == "nepotaskrelatedtags":
	argn = argn + 1
	if not(resws is None):
		for arg in sys.argv[argn:]:
			if arg in ntcommands:
				break
			print arg
			addTagToUrl(arg, resws)
			argn = argn + 1

if len(sys.argv) > argn and not(sys.argv[argn] is None) and sys.argv[argn] == "nepotaskemitnotif":
	msg = (len(notif)>0 and notif) or _("Page not added to current desktop task.")
	subprocess.Popen(args = ["kdialog", "--title", "Nepotask",
				 "--passivepopup", msg, "2"])
